<template>
  <div style="margin:15px" v-loading="loading">
    <div class="filter-container">
      <div style="float:left;margin-left:20px">
        <el-button class="filter-item" style="margin-left: 10px;" type="primary" size="small" icon="el-icon-edit"
                   @click="handleAddClick">添加
        </el-button>
      </div>
    </div>

    <el-table :data="tableData" border stripe highlight-current-row>
      <el-table-column type="index" :index="rowIndexBegin" width="50"></el-table-column>
      <el-table-column header-align="center" align="left" width="50" prop="name" label="姓名"></el-table-column>
      <el-table-column header-align="center" align="left" width="100" prop="description" label="描述"></el-table-column>
      <el-table-column header-align="center" align="left" width="100" prop="fileUril" label="附件"></el-table-column>
      <el-table-column label="操作" width="200">
        <template slot-scope="scope">
          <el-button @click.native="handleEdit(scope.row)" size="small" icon="el-icon-edit">编辑</el-button>
          <el-button @click.native="handleDel(scope.row)" size="small" icon="el-icon-edit">删除</el-button>
        </template>
      </el-table-column>

    </el-table>
    <div style="margin: 20px;overflow: hidden">
      <div style="text-align: center;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="pageCurrent"
          :page-sizes="[5,10,20,50,100]"
          :page-size="pageSize"
          layout="total, prev, pager, next, sizes"
          :total="totalSize"
        >
        </el-pagination>
      </div>
    </div>

    <el-dialog title="添加附件" :visible.sync="isShow" :before-close="closeDialog" width="40%">
      <hr style="height:0.5px;border:none;border-top:1px solid #CCC;margin:-15px 0px 25px 0px">
      <el-form :model="form" label-position="right">
        <el-form-item label="姓名" label-width="80px">
          <el-input v-model="form.name" placeholder="姓名"></el-input>
        </el-form-item>
        <el-form-item label="描述" label-width="80px">
          <el-input v-model="form.description" placeholder="描述"></el-input>
        </el-form-item>
        <el-form-item label="附件上传" label-width="80px" style="float:left">

          <el-upload action="api/mk-upload/contracts/upload"
                     class="upload-demo"
                     :show-file-list="false"
                     :before-upload="beforeUpload">
            <el-button size="small" type="primary">附件上传</el-button>

          </el-upload>
          <span >{{test}}</span>

        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer" label-width="80px">
        <el-button type="primary" @click="onSubmit">确认</el-button>
        <el-button @click="closeDialog"> 取消</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
  import{listData, loadData, saveOrUpdate, deleteData, uploadFile} from 'api/upload'

  export default{
    name: 'upload',
    data(){
      return {
        tableData: [],
        rowIndexBegin: 1,
        loading: false,
        isShow: false,
        form: {},
        pageSize: 20,
        pageCurrent: 1,
        totalSize: -1,
        urlAction: 'http://localhost:8888/api/mk-upload/contracts/upload',
        test:'aaaaasww'


      }
    },
    methods: {
      mockTableData(){
        let page = {page: this.pageCurrent, size: this.pageSize}

        listData(page).then(res => {
          this.tableData = res.data
          console.log()
          this.totalSize = res.totalCount;
        })

      },
      handleAddClick(){
        this.form = {};
        this.isShow = true;
      },

      onSubmit(){
        saveOrUpdate(this.form).then(res => {
          this.mockTableData();
          this.$message('操作成功');
          this.closeDialog();
        })

      },
      closeDialog(){
        this.isShow = false;
      },
      handleEdit(row){
        let data = row;
        this.form = data;
        this.isShow = true;
      },
      handleSizeChange(val){
        this.pageSize = val;

      },
      handleCurrentChange(val){
        this.pageCurrent = val;
        this.mockTableData();
      },
      beforeUpload(file){
        let param = new FormData();
        param.append('file', file, file.name)
        uploadFile(param).then(res => {
          console.log(res)
        })
        return false
      },
      handleDel(row){
        deleteData(row.id).then(res=>{
            this.mockTableData();
            this.$message("操作成功");
        })
      }
    },

    created(){
      this.mockTableData();
    }


  }

</script>
